#pragma once

#include  "iostream"
#include  "vector"
#include  "stack"
#include  "unordered_map"
#include   "queue"

using namespace std;

vector<vector<int>> generateMatrix(int n) {
    vector<vector<int>> res(n, vector<int>(n, 0));
    int count = 1;
    int maxV = n * n;
    int row = 0, col = 0;

    int left = 0, right = n - 1, up = 0, down = n - 1;

    while (count <= maxV && row <= down && col <= right && row >= up && col >= left) {

        //向右边走
        while (col <= right && count <= maxV) {
            res[row][col++] = count++;
        }
        up++;


        col--;

        row++;
        //向下边走
        while (row <= down && count <= maxV) {
            res[row++][col] = count++;
        }
        right--;

        //向左边走
        col--;//
        row--;
        while (col >= left && count <= maxV) {
            res[row][col--] = count++;
        }
        down--;


        row--;
        col++;
        //向上走
        while (row >= up && count <= maxV) {
            res[row--][col] = count++;
        }

        left++;
        row++;
        col++;


    }

    return res;

}